package com.lcifn.utils;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.imageio.ImageIO;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder; 

/**
 * 图片压缩
 * 使用JDK自带工具压缩，压缩质量一般
 * @author lichao
 * @date 2017/12/31
 */
public class ImgCompress {
	
	private static Logger logger = LoggerFactory.getLogger(ImgCompress.class);
	
	private InputStream input;
	private OutputStream output;
	private BufferedImage originImg;
	private int originWidth;
	private int originHeight;
	
	public static void main(String[] args) throws Exception {
		ImgCompress imgCom = new ImgCompress(new FileInputStream(new File("E:\\123.jpg")), 
				new FileOutputStream(new File("E:\\456.jpg")));
		imgCom.resizeFix(1048, 376);
	}
	
	/**
	 * 构造函数
	 */
	public ImgCompress(InputStream input, OutputStream output) {
		this.input = input;
		this.output = output;
		try {
			originImg = ImageIO.read(input);
		} catch (IOException e) {
			logger.error("", e);
			throw new RuntimeException("resize image fail", e);
		}
		originWidth = originImg.getWidth();
		originHeight = originImg.getHeight();
	}
	
	/**
	 * 按照宽度还是高度进行压缩
	 * @param w int 最大宽度
	 * @param h int 最大高度
	 */
	public void resizeFix(int width, int height) {
		if (originWidth / originHeight > width / height) {
			resizeByWidth(height);
		} else {
			resizeByHeight(height);
		}
	}
	
	/**
	 * 以宽度为基准，等比例放缩图片
	 * @param w int 新宽度
	 */
	public void resizeByWidth(int width) {
		int height = (int) (originHeight * width / originWidth);
		resize(width, height);
	}
	
	/**
	 * 以高度为基准，等比例缩放图片
	 * @param h int 新高度
	 */
	public void resizeByHeight(int height){
		int width = (int) (originWidth * height / originHeight);
		resize(width, height);
	}
	
	/**
	 * 强制压缩/放大图片到固定的大小
	 * @param w int 新宽度
	 * @param h int 新高度
	 */
	private void resize(int width, int height) { 
		try {
			logger.debug("start resize image");
			// SCALE_SMOOTH 的缩略算法 生成缩略图片的平滑度的 优先级比速度高 生成的图片质量比较好 但速度慢
			BufferedImage img = new BufferedImage(width, height,BufferedImage.SCALE_SMOOTH); 
			// 绘制缩小后的图
			img.getGraphics().drawImage(originImg, 0, 0, width, height, null); 
			
			// 可以正常实现bmp、png、gif转jpg
			JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(output);
			// JPEG编码
			encoder.encode(img); 
			logger.debug("complete resize image");
		} catch (IOException e) {
			logger.error("", e);
			throw new RuntimeException("resize image fail", e);
		} finally{
			if(input != null){
				try {
					input.close();
				} catch (IOException e) {
					logger.error("", e);
					throw new RuntimeException("input steam close fail", e);
				}
			}
			if(output != null){
				try {
					output.close();
				} catch (IOException e) {
					logger.error("", e);
					throw new RuntimeException("output steam close fail", e);
				}
			}
		}
	}
}
